package com.example.homework3

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.homework3.R

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ArtGalleryApp()
        }
    }
}

@Composable
fun ArtGalleryApp() {
    val artworks = listOf(
        Artwork(R.drawable.gongjin, "公瑾", "作者不详", 2025),
        Artwork(R.drawable.huaimin, "怀民", "作者不详", 2025),
        Artwork(R.drawable.kongming, "孔明", "作者不详", 2025),
        Artwork(R.drawable.yidaqi, "鼬", "作者不详", 2025)
    )

    var currentIndex by remember { mutableIntStateOf(0) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(20.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.SpaceAround
    ) {
        Box(
            modifier = Modifier
                .fillMaxWidth(0.9f)
                .height(500.dp)
                .shadow(8.dp, RoundedCornerShape(16.dp))
                .clip(RoundedCornerShape(16.dp))
        ) {
            Image(
                painter = painterResource(artworks[currentIndex].imageRes),
                contentDescription = artworks[currentIndex].title,
                modifier = Modifier
                    .fillMaxSize()
                    .padding(12.dp)
                    .clip(RoundedCornerShape(12.dp)),
                contentScale = ContentScale.Crop
            )
        }

        Column(
            modifier = Modifier
                .fillMaxWidth(0.8f)
                .padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = artworks[currentIndex].title,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold,
                textAlign = TextAlign.Center,
                color = Color(0xFF4A4A4A)
            )
            Text(
                text = "${artworks[currentIndex].artist} · ${artworks[currentIndex].year}",
                color = Color(0xFF666666),
                modifier = Modifier.padding(top = 4.dp),
                fontSize = 14.sp
            )
        }

        Row(
            modifier = Modifier.fillMaxWidth(0.9f),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Button(
                onClick = {
                    currentIndex = if (currentIndex == 0) artworks.size - 1 else currentIndex - 1
                },
                modifier = Modifier
                    .weight(0.45f)
                    .height(50.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFFAFEEEE)
                ),
                shape = RoundedCornerShape(12.dp)
            ) {
                Text("上一张", fontWeight = FontWeight.Medium)
            }

            Spacer(modifier = Modifier.width(8.dp))

            Button(
                onClick = {
                    currentIndex = (currentIndex + 1) % artworks.size
                },
                modifier = Modifier
                    .weight(0.45f)
                    .height(50.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFFAFEEEE)
                ),
                shape = RoundedCornerShape(12.dp)
            ) {
                Text("下一张", fontWeight = FontWeight.Medium)
            }
        }
    }
}

data class Artwork(
    val imageRes: Int,
    val title: String,
    val artist: String,
    val year: Int
)